﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OOAD_Project_Editor.Domain;
using OOAD_Project_Engine.Foundation.Interfaces;
using OOAD_Project_Editor.Foundation;

namespace OOAD_Project_Editor.Presentation
{
    public partial class SceneStructureForm : Form
    {
        EditorForm editorForm;
        List<IEntity> entities;
        ContextMenuStrip _entityNodeContextMenuStrip;

        public SceneStructureForm(EditorForm editorForm)
        {
            InitializeComponent();
            this.editorForm = editorForm;
            treeView1.Nodes.Clear();
            entities = new List<IEntity>();
            _entityNodeContextMenuStrip = new ContextMenuStrip();
            _entityNodeContextMenuStrip.Opening += OnOpenContextMenu;
        }

        public void SetTreeData(IEditableSceneComponent scene)
        {
            treeView1.Nodes.Clear();
            entities.Clear();
            SetSceneToNode(treeView1.Nodes, scene);
            treeView1.ExpandAll();
        }

        private void SetSceneToNode(TreeNodeCollection nodes, IEditableSceneComponent data)
        {
            TreeNode newNode = new TreeNode("scene");
            nodes.Add(newNode);
            foreach (IEntity e in data.AllEntitys.Values)
                AddEntityNode(newNode.Nodes, e);
        }

        private void AddEntityNode(TreeNodeCollection nodes, IEntity data)
        {
            TreeNode newNode = new TreeNode(data.Name);
            newNode.Tag = data;
            newNode.ContextMenuStrip = _entityNodeContextMenuStrip;
            entities.Add(data);

            nodes.Add(newNode);
            foreach (IEntityComponent c in data)
                AddEntityComponentNode(newNode.Nodes, c);
        }

        private void AddEntityComponentNode(TreeNodeCollection nodes, IEntityComponent data)
        {
            TreeNode newNode = new TreeNode(data.GetType().Name);
            newNode.Tag = data;
            nodes.Add(newNode);
        }

        public void OnOpenContextMenu(object sender, CancelEventArgs e)
        {
            bool cancel = true;
            ContextMenuStrip context = (ContextMenuStrip)sender;
            context.Items.Clear();
            TreeNode treeNode = this.treeView1.SelectedNode;
            if (treeNode != null)
            {
                if (treeNode.Tag is IEntity)
                {
                    initializeEntityContextMenu(context, treeNode.Tag as IEntity);
                    cancel = false;
                }
            }
            e.Cancel = cancel;
        }

        private void initializeEntityContextMenu(ContextMenuStrip contextMenuStrip, IEntity entity)
        {
            ToolStripItem item = createItem(entity, "AddComponent");
            item.Click += addEntityComponentAction;
            contextMenuStrip.Items.Add(item);

            item = createItem(entity, "RemoveEntity");
            item.Click += removeEntityAction;
            contextMenuStrip.Items.Add(item);
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.treeView1.SelectedNode = e.Node;
            if (e.Node.Tag != null && e.Button == MouseButtons.Left)
            {
                if (e.Node.Tag is IEntity)
                {
                    IEntity entity = e.Node.Tag as IEntity;
                    editorForm.ManageSceneHandler.SelectEntityByName(entity.Name);
                }
            }
        }

        private ToolStripItem createItem(IEntity entity, string text)
        {
            ToolStripItem item = new ToolStripMenuItem();
            item.Name = text;
            item.Text = text;
            item.Tag = entity;
            return item;
        }

        private void addEntityComponentAction(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            IEntity entity = item.Tag as IEntity;
            editorForm.AddEntityComponent(entity);
        }

        private void removeEntityAction(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            IEntity entity = item.Tag as IEntity;
            editorForm.RemoveEntity(entity);
        }
    }
}
